package com.example.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.example.service.FileService;

@Controller
public class FileController {

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);
    private final FileService fileService;

    // 构造函数注入
    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @PostMapping("/file/rename")
    @ResponseBody
    public Map<String, Object> renameFiles(
        @RequestParam("fileIds") String fileIdsStr,
        @RequestParam("namePattern") String namePattern,
        @RequestParam("startNumber") int startNumber) {

        Map<String, Object> result = new HashMap<>();
        try {
            // 解析文件ID
            List<Long> fileIds = Arrays.stream(fileIdsStr.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());

            // 执行重命名操作
            fileService.renameFiles(fileIds, namePattern, startNumber);

            result.put("success", true);
            result.put("message", "重命名成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            logger.error("重命名失败", e);
        }
        return result;
    }
}